package com.wzcl.app.controller;

import com.wzcl.app.dao.model.FileModel;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.FileModelDto;
import com.wzcl.app.model.vo.FileModelVo;
import com.wzcl.app.service.FileModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/fileModel")
@Api(value = "素材管理controller", tags = {"素材管理接口"})
public class FileModelController{

    @Resource
    private FileModelService service;

    @ApiOperation("添加素材")
    @PostMapping("/add")
    public RVO<FileModelVo> addFileModel(@RequestBody @Valid FileModelDto fileModelDto){
        FileModel fileModel = service.addFileModel(fileModelDto);
        return RVO.success(FileModelVo.create(fileModel));
    }

    @ApiOperation("修改素材")
    @PostMapping("/update")
    public RVO<FileModelVo> updateFileModel(@RequestBody FileModelDto fileModelDto){
        FileModel fileModel = service.updateFileModel(fileModelDto);
        return RVO.success(FileModelVo.create(fileModel));
    }

    @ApiOperation("删除素材")
    @PostMapping("/del")
    public RVO<String> delFileMode(@RequestBody FileModelDto fileModelDto){
        service.delFileModel(fileModelDto);
        return RVO.success();
    }

    @ApiOperation("移动素材")
    @PostMapping("/move")
    public RVO<String> moveFileMode(@RequestBody FileModelDto fileModelDto){
        service.moveFileModel(fileModelDto);
        return RVO.success();
    }

    @ApiOperation("拷贝素材")
    @PostMapping("/copy")
    public RVO<String> copyFileMode(@RequestBody FileModelDto fileModelDto){
        service.copyFileModel(fileModelDto);
        return RVO.success();
    }

    @ApiOperation("获得素材信息")
    @PostMapping("/getList")
    public RVO<List<FileModelVo>> getFileModeList(@RequestBody FileModelDto fileModelDto){
        List<FileModel> fileModelList = service.getFileModelList(fileModelDto);
        if(fileModelList != null && fileModelList.size()>0){
            List<FileModelVo> collect = fileModelList.stream().map(FileModelVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获得素材信息")
    @PostMapping("/getPage")
    public RVO<List<FileModelVo>> getFileModePage(@RequestBody FileModelDto fileModelDto){
        MyPage<FileModel> page = service.getFileModelPage(fileModelDto);
        if(page != null && page.getContent().size()>0){
            List<FileModelVo> collect = page.getContent().stream().map(FileModelVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }


}
